﻿using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Collections.Specialized;

/// <summary>
/// Summary description for Mailer
/// </summary>
public class Mailer
{
	public Mailer()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public enum MessageTemplate
    {
        ChangePassword,  
        NewOrder,
       Email,
        Suggestion,
        SuggestionUser, 
        ReferLink
    }

    public static System.Net.Mail.MailMessage CreateMessage(WebControl owner, MessageTemplate messageTemplate, ListDictionary replacements, string to, string subject)
    {
        string physicalPath = HttpContext.Current.Server.MapPath("~/EmailTemplates/" + messageTemplate.ToString() + ".txt");

        if (!File.Exists(physicalPath))
        {
            throw new ArgumentException("Invalid MailTemplate Passed into the NotificationManager", "messageTemplate");
        }

        MailDefinition md = new MailDefinition();
        md.BodyFileName = physicalPath;
        md.Subject = subject;
        
        md.From =System.Configuration.ConfigurationManager.AppSettings["EmailFrom"].ToString();
        //md.CC = cc;
        md.IsBodyHtml = true;

        System.Net.Mail.MailMessage mailMessage;
        //this actually processes all of the replacement vars that you've included.
        mailMessage = md.CreateMailMessage(to, replacements, new TextBox());
        return mailMessage;
    }

    public static System.Net.Mail.MailMessage CreateMessageToAdmin(WebControl owner, MessageTemplate messageTemplate, ListDictionary replacements, string subject)
    {
        string physicalPath = HttpContext.Current.Server.MapPath("~/EmailTemplates/" + messageTemplate.ToString() + ".txt");

        if (!File.Exists(physicalPath))
        {
            throw new ArgumentException("Invalid MailTemplate Passed into the NotificationManager", "messageTemplate");
        }

        MailDefinition md = new MailDefinition();
        md.BodyFileName = physicalPath;
        md.Subject = subject;
        md.From = System.Configuration.ConfigurationManager.AppSettings["EmailFrom"].ToString();
        //md.CC = cc;
        md.IsBodyHtml = false;

        string to = System.Configuration.ConfigurationManager.AppSettings["AdminNotificationAddresses"].ToString();

        System.Net.Mail.MailMessage mailMessage;
        //this actually processes all of the replacement vars that you've included.
        mailMessage = md.CreateMailMessage(to, replacements, new TextBox());
        return mailMessage;
    }

    //public static bool SendMessage(WebControl owner, MessageTemplate messageTemplate, ListDictionary replacements, string to, string subject)
    //{
    //    MailMessage mailMessage = CreateMessage(owner, messageTemplate, replacements, to, null, null, subject);
    //    return InternalSend(mailMessage, true);
    //}

    public static bool InternalSend(MailMessage mailMessage, bool isAsync)
    {
        try
        {
            if (!isAsync)  // blocks current thread
                Mailer.SendMailMessage(mailMessage);
            else  // does not block current thread
                Mailer.SendMailMessageAsync(mailMessage);
        }
        catch (Exception exc)
        {
            //handle, log, do something
            throw;
        }
        return true;
    }

    private static bool SendMailMessage(MailMessage mailMessage)
    {
        try
        {
            SmtpClient client = new SmtpClient();
            client.Host = System.Configuration.ConfigurationManager.AppSettings["SMTPServer"].ToString();
            client.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["SendingEmailAccountUsername"].ToString(), System.Configuration.ConfigurationManager.AppSettings["SendingEmailAccountPassword"].ToString());

            //Default port will be 25
            client.Port = 25;

            client.Send(mailMessage);
        }
        catch (Exception exc)
        {
            // Log
            throw;
        }
        return true;
    }

    private static void SendMailMessageAsync(MailMessage mailMessage)
    {
        try
        {
            SmtpClient client = new SmtpClient();
            client.Host = System.Configuration.ConfigurationManager.AppSettings["SMTPServer"].ToString();
            client.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["SendingEmailAccountUsername"].ToString(), System.Configuration.ConfigurationManager.AppSettings["SendingEmailAccountPassword"].ToString());

            //Default port will be 25
            client.Port = 25;

            client.SendAsync(mailMessage, mailMessage.To.ToString());
        }
        catch (Exception)
        {
            //Log
            throw;
        }
    }

}
